package com.aaa.demo09;

import com.aaa.demo03.MyTicket;

import java.util.Arrays;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ${崔灿}
 * @date 2022/11/01 17:22
 **/

public class MyTicketTwo {
    //private static int ticket=1000;
    private static Object lock= new Object();
    public static void main(String[] args) throws InterruptedException {
        MyTicket1 myTicket1 = new MyTicket1();
        Thread thread1 = new Thread(myTicket1, "窗口A");
        Thread thread2 = new Thread(myTicket1, "窗口B");
        Thread thread3 = new Thread(myTicket1, "窗口C");
        Thread thread4 = new Thread(myTicket1, "窗口D");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
class MyTicket1 implements Runnable{
    private static int ticket=100;
    private static Object ticket1=new Object();
    @Override
    public void run() {

        while (true) {
            synchronized (ticket1) {
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖了一张票，剩余"+--ticket );
                }
                else {
                    break;
                }
            }
        }
    }
}
